# -*- coding: utf-8 -*-
from billing.models import HourRecord,DayRecord
from django.db.models import Sum
import time
import datetime
import logging
log=logging.getLogger('django')

def generate_days_consumption():
    
    now = datetime.datetime.now()
    endTime = now + datetime.timedelta(days=-1)
    print now 
    print "----日定时任务开始----"
    try:
        dayRecordList = HourRecord.objects.filter(record_time__gte = endTime).values("project_id").annotate(
            cpu_use = Sum("cpu_use"),disk_use=Sum("disk_use"),memory_use=Sum("memory_use"),cpu_consume=Sum("cpu_consume"),disk_consume=Sum("disk_consume"),memory_consume=Sum("memory_consume")
            ).values('project_id','cpu_consume','disk_consume','memory_consume','cpu_use','disk_use','memory_use')
        for record in dayRecordList:
            try:
                dayTemp = DayRecord(project_id=record['project_id'],cpu_consume = record['cpu_consume'],
                                    disk_consume = record['disk_consume'],memory_consume= record['memory_consume'],
                                    cpu_use= record['cpu_use'],memory_use=record['memory_use'],disk_use = record['disk_use'])
                dayTemp.save()
            except:
                print "保存数据异常"
    except:
        print "异常"
        
    print "----日定时任务结束----"
    print datetime.datetime.now()